# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=mesonlsp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.7
pkgrel=3
pkgdesc='Meson language server (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url=https://github.com/JCWasmx86/mesonlsp
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-pkgconf"
         "${MINGW_PACKAGE_PREFIX}-libtree-sitter"
         "${MINGW_PACKAGE_PREFIX}-dlfcn"
         "${MINGW_PACKAGE_PREFIX}-tomlplusplus"
         'git'
         'mercurial'
         'subversion')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-gtest")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-lsprotocol"
              "${MINGW_PACKAGE_PREFIX}-python-pygls")
source=("git+${url}.git#tag=v${pkgver}"
        "git+https://github.com/ada-url/ada#tag=v2.9.2"
        "sha256::git+https://github.com/amosnier/sha-2#commit=49265c656f9b370da660531db8cc6bf0a2e110a6"
        "git+https://github.com/JCWasmx86/muon#commit=c44ff3d842eb9fb738b14e3c6e23693290c61601"
        "git+https://github.com/JCWasmx86/tree-sitter-ini#commit=848b6269f7039739aebd169fbd3d5e6e34bef661"
        "git+https://github.com/JCWasmx86/tree-sitter-meson#commit=09665faff74548820c10d77dd8738cd76d488572")
sha256sums=('dbddb485de460a1bfc0efb32d76517e589229e0e1a571aaa15e9700f871127b2'
            '4a796665d17f4b050fd37395492f2fecb06cf98a3229a099b9b3786bbc137acc'
            '324f8510ec22aa04350183bb37263c03fd81ad708fcbc0b82056f558db753cc7'
            '1fc46d8a15237ce5ec6803469e10a4545ce18ab2811bc0441d6901b22b0cba97'
            'f6b7d4d26025b5e4c6abb6bc858254501ceddaa69b4435002d612abaa8a67b84'
            'dfd1f1c88b727dfbe535446bb8e1fec2a17c410b5455a9a70dfce3f8d76c9380')

prepare() {
  cd "${_realname}"

  # apply patches that also fix build
  git cherry-pick -n 8edb5af16db9d7651a88ae19d0574195a02e40b6
  git cherry-pick -n 851eb422851406ca4221e7bd866cd97b51c9ac06
}

build() {
  export MESON_PACKAGE_CACHE_DIR="${srcdir}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson setup \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      -Dbenchmarks=false \
      -Duse_own_tree_sitter=false \
      "build-${MSYSTEM}" \
      "${_realname}"

  meson compile -C "build-${MSYSTEM}"
}

package() {
  meson install -C "build-${MSYSTEM}" --destdir "${pkgdir}"
  rm "${pkgdir}${MINGW_PREFIX}"/bin/{Swift-MesonLSP,muon}

  install -Dm644 "${_realname}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
